#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

import gdata.blogger.client
import gdata.client
import gdata.data
import atom.data


class Blog(webapp.RequestHandler):
  def __init__(self):
    categories = ["lang:en", "news"]
    self.client = gdata.blogger.client.BloggerClient()
    self.query = gdata.blogger.client.Query(categories=categories, max_results=10, strict=True, order_by="updated")
    self.feed = self.client.GetPosts(blog_id="7297489546734971425", query=self.query)
  def get(self):
    results = []
    if self.feed.entry:
      for entry in self.feed.entry:
        result = {}
        result["title"] = entry.title.text
        result["body"] = entry.content.text
        results.append(result)
    self.response.out.write(template.render(os.path.join(os.path.dirname(__file__), "blog.html"), {"results":results}))


def main():
  run_wsgi_app(webapp.WSGIApplication([("/.*", Blog)], debug=False))


if __name__ == "__main__":
  main()
